package com.example.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

/**
 * @Desc: 格式化首字母
 * @Project NAME: campin.bomesc.unit   
 * @Class NAME: XMLResUtil 
 * @Author: Kangping & Campin
 * @Link: kangpingchn@hotmail.com
 * @Date: Nov 10, 2020 2:22:05 PM      
 * @Vesion: v2020.12.11.0
 *
 * Copyright ©2020 ZHOUKANGPING. All rights reserved.
 */
public class XMLResUtil {
	/**
	 * @Title: ResBPExchange 
	 * @Description: 处理首字母（小写转大写，排除特别定义）
	 * @param @param 对xml处理
	 * @param @param clazz 需要格式化的对象类
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static String ResBPExchange(String xml, Class clazz) {
		// 根据对象属性自动将将首字母转大写以匹配UNIFIER实际要素首字母命名方式
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			if (propertyDescriptors != null && propertyDescriptors.length > 0) {
				for (PropertyDescriptor pd : propertyDescriptors) {
					xml = xml.replaceAll(pd.getName(), to1stUpper(pd.getName()));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 将NA及N/A内容为空
		xml = xml.replaceAll("NA", "");
		xml = xml.replaceAll("N/A", "");

		return xml;
	}

	/**
	 * @Title: to1stUpper 
	 * @Description: 建立列表排除需要格式首字母的要素，大多数为系统自带要素
	 * @param @param str
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static String to1stUpper(String str) {
		String sysElement1 = "umatSKUMtrDesc-ugenProjectNumber-ugenProjectName-record_no-title-due_date-end_date-status-"
				+ "creator_id-currencyid-uuu_proj_currency-otherCompanyID-amount-short_desc-description-bItemID-wpid-k__Material_Code_DP"
				+ "refid-ref_rfb-ref_bpo-ref_bpo_lineitem-row_id-scheduled_value-uCoveragetoDOP-uirCntctFstNmTB-udrawRevNoBP"
				+ "ufmMeterUOMPD-uLinStruName-uvePrimaryContactTB64";
		char[] chars = str.toCharArray();	
		
		if ((chars[0] >= 'a' && chars[0] <= 'z') && (!sysElement1.contains(str)) && (!str.substring(0, 3).contentEquals("uuu"))) {
			chars[0] = (char) (chars[0] - 32);
		}

		return new String(chars);
	}

}
